package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Driver;
import com.example.vehiclemanagesystem.entity.User;
import com.example.vehiclemanagesystem.entity.Vehicle;
import com.example.vehiclemanagesystem.service.DriverService;
import com.example.vehiclemanagesystem.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DriverController {
    @Autowired
    private DriverService driverService;

    @PostMapping("/addDriver")
    public Map<String, Object> addDriver(@RequestBody Map<String, String> remap) {
        Integer d_id = Integer.parseInt(remap.get("d_id"));
        String name = remap.get("name");
        String type = remap.get("type");
        String state = remap.get("state");
        String punish = remap.get("punish");
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver = new Driver(d_id, name, type, state, punish);
            Driver driver1 = driverService.selectDriverByDriverID(d_id);
            if (driver1 != null) {
                map.put("success", false);
                map.put("message", "司机已存在");
            } else {
                driverService.addNewDriver(driver);
                map.put("success", true);
                map.put("message", "添加成功 ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "添加失败！");
        }
        return map;
    }
    /*
    @GetMapping("/allDriver")
    public List<Driver> printAll(){
        List<Driver> driverList = driverService.allDriver();
        return driverList;
    }*/
    @RequestMapping("/allDriver")
    public Map<String,Object> printDriver(){
        List<Driver> driverList = driverService.allDriver();
        Map<String, Object> map = new HashMap<>();
        map.put("整个Driver表",driverList);
        map.put("pagenum",1);
        map.put("pagesize",1);
        return map;
    }
    @RequestMapping("/pageDriver")
    public Map<String,Object> PRINTbypage(){
        Map<String, Object> map = new HashMap<>();
        List<Driver> driverList = driverService.findByPageF(1, 1);
        map.put("分页显示",driverList);
        return map;
    }

    /*@RequestMapping("/searchDriver")
    public Map<String,Object> thisDriver(@RequestBody Map<String,String>remap){
        Integer d_id = Integer.parseInt(remap.get("d_id"));
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver = driverService.selectDriverByDriverID(d_id);
            if (driver == null) {
                map.put("success", false);
                map.put("message", "司机不存在");
            } else {

                map.put("success", true);
                map.put("message","搜索成功");
                map.put("搜索到的司机",driver);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    */
    @RequestMapping("/searchDriver")
    public Map<String, Object> thisDriver(@RequestParam("d_id") String input) {
        Map<String, Object> map = new HashMap<>();
        if (input == null || input.equals("")) {
            List<Driver> driverList = driverService.allDriver();
            map.put("pagenum", 1);
            int total = driverList.size();
            map.put("total", total);
            map.put("success", true);
            map.put("搜索到的司机", driverList);
            return map;
        }
        Integer d_id;
        try {
            d_id = Integer.parseInt(input);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "输入不合法");
            return map;
        }
        Driver driver=driverService.selectDriverByDriverID(d_id);
        ArrayList<Driver> drivers=new ArrayList<>();
        drivers.add(driver);
        if (driver == null) {
            map.put("success", false);
            map.put("message", "人员不存在");
        } else {
            map.put("success", true);
            map.put("message",/*d_id+*/"搜索成功");
            map.put("搜索到的司机", drivers);
        }
        return map;
    }


    @PostMapping("/deleteDriver")
    public Map<String, Object> deleteDriver(@RequestBody String input) {
        input = input.substring(0,input.length()-1);
        System.out.println(input);
        Integer d_id = Integer.parseInt(input);
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver = driverService.selectDriverByDriverID(d_id);
            if (driver == null) {
                map.put("success", false);
                map.put("message", "司机不存在");
            } else {
                driverService.deleteDriver(driver);
                map.put("success", true);
                map.put("message",/*d_id+*/"司机删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    @PostMapping("/updateDriver")
    public Map<String, Object> updateDriver(@RequestBody Map<String, String> remap) {
        Integer d_id = Integer.parseInt(remap.get("d_id"));
        String name = remap.get("name");
        String type = remap.get("type");
        String state = remap.get("state");
        String punish = remap.get("punish");
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver1 = driverService.selectDriverByDriverID(d_id);
            if (driver1 == null) {
                map.put("success", false);
                map.put("message", "司机不存在");
            } else {
                Driver driver = new Driver(d_id, name, type, state, punish);
                driverService.updateDriver(driver);
                map.put("success", true);
                map.put("message",/*d_id+*/"司机修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    @PostMapping("/suspendDriver")
    public Map<String, Object> suspendDriver(@RequestBody Map<String, String> remap)
    {
        Integer d_id = Integer.parseInt(remap.get("d_id"));
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver1 = driverService.selectDriverByDriverID(d_id);

            if (driver1 == null) {
                map.put("success", false);
                map.put("message", "司机不存在");
            } else {
                String name=driver1.getName();
                String type=driver1.getType();
                String state="停职";
                String punish=driver1.getPunish();
                Driver driver = new Driver(d_id, name, type, state, punish);
                driverService.updateDriver(driver);
                map.put("success", true);
                map.put("message",/*d_id+*/"司机停职成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }

    @PostMapping("/workDriver")
    public Map<String, Object> workDriver(@RequestBody Map<String, String> remap)
    {
        Integer d_id = Integer.parseInt(remap.get("d_id"));
        Map<String, Object> map = new HashMap<>();
        try {
            Driver driver1 = driverService.selectDriverByDriverID(d_id);

            if (driver1 == null) {
                map.put("success", false);
                map.put("message", "司机不存在");
            } else {
                String name=driver1.getName();
                String type=driver1.getType();
                String state="工作";
                String punish=driver1.getPunish();
                Driver driver = new Driver(d_id, name, type, state, punish);
                driverService.updateDriver(driver);
                map.put("success", true);
                map.put("message",/*d_id+*/"司机解除停职");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }


    @RequestMapping("/getDriverList")
    public List<Driver> getDriverList() {
            List<Driver> driverList = driverService.allDriver();
        return driverList;
    }

    @RequestMapping("/getDriverLists")
    public List<Map<String,Object>> getDriverLists() {
        List<Driver> driverList = driverService.allDriver();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Driver driver : driverList){
            Map<String,Object> map = new HashMap<>();
            map.put("d_id",driver.getD_id());
            map.put("name",driver.getName());
            //map.put("type","vehicle");
            mapList.add(map);
        }
        return mapList;
    }
}
